package us.sunrisemorning.pixivbrower.dao;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.plugin.activerecord.Db;

import us.sunrisemorning.pixivbrower.model.Tag;

public class TagService {
    public final Tag dao = new Tag().dao();

    public synchronized void updateTag(int illustId, JSONArray tags) {
        Db.delete("delete from tag where illustid=?", illustId);

        List<Tag> list = new ArrayList<Tag>();
        tags.forEach(obj -> {
            String tagName = (String) obj;

            Tag tag = new Tag();
            tag.setIllustid(illustId);
            tag.setTagname(tagName);
            list.add(tag);
        });
        
        Db.batchSave(list, list.size());
    }

    public List<String> findAllTagsByIllustId(int illustId) {
        List<Tag> tags = dao.find("select * from tag where illustid=?", illustId);
        List<String> results = new ArrayList<String>();
        tags.forEach(tag -> {
            results.add(tag.getTagname());
        });
        return results;
    }
}
